package com.evl.api.ticketCheckStation.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.evl.api.ticketCheckStation.service.ITicketCheckStationService;
import com.evl.common.persistence.dao.ScheduleStationMapper;
import com.evl.common.persistence.dao.TicketCheckStationMapper;
import com.evl.common.persistence.model.ScheduleStation;
import com.evl.common.persistence.model.TicketCheckStation;
import com.evl.common.persistence.model.TrainTicket;

@Service
public class TicketCheckStationServiceImpl implements ITicketCheckStationService {

	@Resource
	ScheduleStationMapper scheduleStationMapper;

	@Resource
	TicketCheckStationMapper ticketCheckStationMapper;
	
	@Override
	public List<TicketCheckStation> getCheckStations(String ticketId)
			throws Exception {
		
		return ticketCheckStationMapper.selectStations(ticketId);
	}
	
	@Override
	public void checkTicket(String id, String openid) {
		ticketCheckStationMapper.checkTicket(id,openid);
	}

	@Override
	public void createCheckStations(List<TrainTicket> list, Integer staId)
			throws Exception {
		
		List<TicketCheckStation> resultList = new ArrayList<TicketCheckStation>();
		
		//获取当前车次的所有站点信息
		List<ScheduleStation> ssList = scheduleStationMapper.selectScheduleStations(staId);
		
		//循环封装检票站点信息
		for(TrainTicket trainTicket :list){
			for(ScheduleStation scheduleStation : ssList){
				TicketCheckStation  ticketCheckStation = new TicketCheckStation();
				ticketCheckStation.setTicketId(Long.parseLong(String.valueOf(trainTicket.getId())));//车票id
				ticketCheckStation.setStaId(staId);//车次Id
				ticketCheckStation.setSeq(scheduleStation.getSeq());//车站序号
				ticketCheckStation.setStationName(scheduleStation.getStationName());//车站名称
				ticketCheckStation.setStartTime(scheduleStation.getStartTime());//车站发车时间
				ticketCheckStation.setStatus("0");//未检票
				
				resultList.add(ticketCheckStation);
			}
		}
		//保存检票站点信息
		if(resultList.size()>0){
			ticketCheckStationMapper.batchInsert(resultList);
		}
		
	}}
